package second.demo1;

import java.lang.reflect.Field;

/**
 * @Description:
 * @author:LiWei
 * @date:2022年10月27日12:20
 */
public class Test {
    public static void main(String[] args) throws Exception{
        //通过类名获取反射类对象
        Class<Student> studentClass= Student.class;

//        Student student1 = studentClass.newInstance();
//        Student student2 = studentClass.newInstance();
//
//        System.out.println(student1==student2);

//        Field field = studentClass.getDeclaredField("name");
//        System.out.println(field);
//        Field age = studentClass.getDeclaredField("age");
//        System.out.println(age);

        //获取所有属性
//        Field[] declaredField = studentClass.getDeclaredFields();
//        for (Field field1 : declaredField) {
//            System.out.println(field1);
//        }

        //获取本类以及父类中指定的public属性
        Field name = studentClass.getField("name");
        System.out.println(name);
        Field address = studentClass.getField("address");
        System.out.println(address);
    }
}

class People{
    public String address;
    private String sex;

}

class Student extends People {
    public String name;
    private int age;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}